<?php
namespace app\test\controller;
use think\Db;
use think\Request;
use think\Controller;
use think\Session;
use think\Cookie;
use app\rest\model\User as UserModel;

class Index extends Controller
{
    public function _initialize(){
        $session = session::get();
        if(empty($session['uid'])){
            return $this->adminLogin();
        }

        $c = Request::instance()->controller();
//        var_dump($c);
        $action = Request::instance()->action();


        foreach($session['qx'] as $k=>$v){

//            var_dump($k);
//            var_dump($v);

        }


        if ($c != $k) {
            echo '你没登录权限,给老子滚';
        }

//        var_dump($action,$v);
        if(in_array($action, $v)){
            return true;
        }else{
            return $this->error('你没有权限,给老子滚');
        }


    }


    public function index()
    {
        $cookie = cookie::get();
//        var_dump($cookie);
        if(!empty($cookie['uname'])){
            $data = Db::name('user')->where('name',$cookie['uname'])->find();
            if($data['check']==2){
                return $this->success('免登录','/shouye');
            }

        }else{

//            return view('/');
          return $this->adminLogin();

        }
    }

    public function adminLogin()
    {
        return view('logins/index');
    }




    public function shouye()
    {

        return view('index/index');
    }



    public function list()
    {


        $list = Db::name('user')->field(['id', 'name'])->order('id', 'ASC')->select();
//        var_dump($list);
        return view('user/list',[
            'list'=>$list
        ]);
    }


    //删除用户
    public function delete($id)
    {
        $result = Db::name('user')->delete($id);

        if ($result > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为' . $id . '的用户删除成功';

        } else {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = '删除失败';
        }
        return json($info);
    }

    //删除角色
    public function delRoles($id)
    {
        $result = Db::name('roles')->delete($id);

        if ($result > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为' . $id . '的角色删除成功';

        } else {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = '删除失败';
        }
        return json($info);
    }

    //删除权限
    public function delpre($id)
    {
        $result = Db::name('pre')->delete($id);

        if ($result > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为' . $id . '的权限删除成功';

        } else {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = '删除失败';
        }
        return json($info);
    }

    public function save(Request $request)
    {

//        var_dump($request->post());
        $p = input('post.');
        $p['pass']= password_hash($p['pass'],1);

        $data =[
            'name'=>$p['name'],
            'pass'=>$p['pass'],
        ];

        $result = Db::name('user')->data($data)->insert();

        if ($result > 0) {
           return $this->success('安排上了','/list');
        } else {
            return $this->error('添加失败','/adduser');

        }
    }

    public function doaddroles(Request $request)
    {
//
        $p = input('post.');


        $result = Db::name('roles')->data($p)->insert();

        if ($result > 0) {
            return $this->success('安排上了');
        } else {
            return $this->error('添加失败');

        }
    }

    public function doassignroles(Request $request)
    {

        $data = input('post.');
//        var_dump($data);

        $result = Db::name('user_roles')->data($data)->insert();

        if ($result > 0) {
            return $this->success('安排上了');
        } else {
            return $this->error('添加失败');

        }
    }

    public function doassignper(Request $request)
    {

        $data = input('post.');
//        var_dump($data);
        $result = Db::name('role_pre')->data($data)->insert();

        if ($result > 0) {
            return $this->success('安排上了');
        } else {
            return $this->error('添加失败');

        }
    }

    public function doaddpre(Request $request)
    {
        $data = input('post.');
//        var_dump($data);
        $result = Db::name('pre')->data($data)->insert();

        if ($result > 0) {
            return $this->success('安排上了');
        } else {
            return $this->error('添加失败');
        }
    }





    public function form()
    {
        return view('user/form');
    }




#用户模块
    //添加用户
    public function user()
    {
        return view('user/adduser');
    }

    //分配角色
    public function assignroles()
    {
        $list = Db::name( 'user')->field(['id', 'name'])->order('id', 'ASC')->select();
//        var_dump($list);

        $res = Db::name('roles')->field(['id', 'role_name'])->order('id', 'ASC')->select();
//        var_dump($res);


        return view('user/assignroles',[
            'list'=>$list,
            'res'=>$res
        ]);
    }


#角色模块  roles=>角色


    //角色添加
    public function addroles()
    {
//        echo 123;die;
        return view('roles/addroles');
    }


    //查询角色 query查询
    public function queryroles()
    {

        $list = Db::name('roles')->field(['id', 'role_name'])->order('id', 'ASC')->select();

        return view('roles/queryroles',[
            'list'=>$list
        ]);
    }


    //分配权限 assign分配
    public function assignper()
    {
        $list = Db::name( 'pre')->field(['id', 'pre_name','controller','function'])->order('id', 'ASC')->select();
        $res = Db::name('roles')->field(['id', 'role_name'])->order('id', 'ASC')->select();

        return view('roles/assignper',[
            'list'=>$list,
            'res'=>$res
        ]);

    }


#权限模块  pre =>权限

    //添加权限
    public function addpre()
    {

        return view('pre/addpre');
    }

    //查询权限
    public function querypre()
    {
        $list = Db::name('pre')->field(['id', 'pre_name','controller','function'])->order('id', 'ASC')->select();

//        var_dump($list);die;
        return view('pre/querypre',[
            'list'=>$list
        ]);

    }


    //节点
    public function assignNode()
    {
        // 角色信息
        $list = Db::name('roles')->field(['role_name','id'])->order('id','ASC')->select();

        return view('pre/assignnode',[
            'list'=>$list
        ]);
    }

    public function showRoles($id)
    {

//        $row = Db::field('pre_name')
//            ->table(['xy_role_pre','xy_pre'])
//            ->where('xy_role_pre.pid','=','xy_pre.id')
//            ->where( 'xy_role_pre.pid','=',37)
//            ->select();
        $row = Db::table('xy_pre')
            ->alias('p')
            ->field('p.pre_name,p.id')
            ->join('xy_role_pre rp', 'rp.pid = p.id')
            ->where('rp.rid',$id)
            ->select();
//        dump($row);exit();
        //查出我当前选择的 角色 已经有的 权限id
        return json($row);
    }

    public function showNode()
    {

        $list = Db::name('roles')->field(['role_name','id'])->order('id','ASC')->select();
        $res = Db::name('pre')->field(['pre_name','id'])->order('id','ASC')->select();
//        var_dump($res);die;

        return view('pre/donode',[
            'list'=>$list,
            'res'=>$res,
        ]);


    }


    public function addpreajax(Request $request)
    {
//
        $p = input('post.');
//        var_dump($p);die;

        $result = Db::name('role_pre')->data($p)->insert();

        if ($result > 0) {
            return $this->success('安排上了');
        } else {
            return $this->error('添加失败');

        }
    }

   public function delpreajax(Request $request)
    {

        $p = input('post.');

        $result = Db::name('role_pre')->where('pid',$p['pid'])->where('rid',$p['rid'])->delete();

        if ($result > 0) {
            return $this->success('安排上了');
        } else {
            return $this->error('添加失败');

        }
    }


    //做登录

    public function dologins(Request $request)
    {

            $p = input('post.');
    //        var_dump($p['pass']);
            $data = Db::name('user')->where('name',$p['name'])->find();
    //        var_dump($data['pass']);
            if(!password_verify($p['pass'],$data['pass'])){
                return $this->error( '密码错误');
            }


            if(!empty($p['check'])){
                $result = Db::table('xy_user')->where(['name'=>$data['name']])->update(['check'=>2]);

    //            var_dump($data);
                cookie::set('uid',$data['id']);
                cookie::set('uname',$data['name']);
                cookie::set('upass',$data['pass']);

//                var_dump($result);
            }else{
                $result = Db::table('xy_user')->where(['name'=>$data['name']])->update(['check'=>1]);
//                var_dump($result);
            }

            if ($data['id']) {
                session::set('uname',$data['name']);
                session::set('upass',$data['pass']);
                session::set('uid',$data['id']);
                $this ->session($data['id']);
                return $this->success('安排上了','/shouye');
            } else {
                return $this->error('安排不上了');
            }



    }

    //做session
    public function session($uid)
    {

//        var_dump($uid);

        $list = Db::view('pre','controller,function')
            ->view('role_pre','pid','pre.id=role_pre.pid')
            ->view('user_roles','rid','role_pre.rid=user_roles.rid')
            ->view('xy_user','id','xy_user.id=user_roles.uid')
            ->where('id','=',$uid)
            ->select();
//        var_dump($list);die;


        foreach ($list as $key => $val) {
            $list[$key]['controller'] = ucfirst($val['controller']);
        }

        // var_dump($list);die;

        $nodelist = array();
        foreach($list as $v){
            $nodelist[$v['controller']][] = $v['function'];
        }

//         var_dump($nodelist);die;
        Session::set('qx',$nodelist);

//        var_dump(Session::get());die;


//        $rid = Db::name('user_roles')
//            ->field('rid')
//            ->where('uid','=',$uid)
//            ->select();
//
//        session::set('roles',$rid);
//
//        // 如果使用三维数组
//        $pid =[];
//        foreach($rid as $k=>$v){
//            $conent = Db::name('role_pre')
//                ->field('pid')
//                ->where('rid','=',$v['rid'])
//                ->select();
//
//            $pid[$v['rid']] = $conent;
//        }
//        //        var_dump($pid);
//
//
////        var_dump($pid);
//        // 如果要使用二维数组
//        $res=[];
//        foreach($pid as $k=>$v){
//            foreach($v as $k=>$value){
//                $result[] = $value;
//            }
//        }
////        var_dump($result);
//
//
//        session::set('pid',$result);


    }



}
